﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;

namespace WindowsFormsApplication
{
    public partial class frmProducts : DevExpress.XtraEditors.XtraForm
    {
        qltkDb db = new qltkDb();

        public frmProducts()
        {
            InitializeComponent();
            this.ShowIcon = false;
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.Sizable;
        }

        private void InitGridProducts()
        {
            //var products = db.SO_HANGs;

            var products = from p in db.SO_HANGs
                           select new
                           {
                               MaHH = p.MaHH,
                               TenHang = p.TenHang,
                               DVT = p.DVT,
                               PhePham = p.PhePham,
                               TenNhom = p.DM_NHOM_HANG.TenNhom,
                               NSX = p.NSX,
                               MaNhom = p.MaNhom,
                           };

            gridControl1.DataSource = products;
            
            gridView1.Columns[0].Caption = "Mã hàng";
            //gridView1.Columns[0].OptionsColumn.ReadOnly = true;
            //gridView1.Columns[0].OptionsColumn.AllowEdit = false;

            gridView1.Columns[1].Caption = "Tên hàng";
            gridView1.Columns[3].Caption = "Phế phẩm";

            gridView1.Columns[4].Caption = "Nhóm";
            //gridView1.Columns[4].ColumnEdit = repositoryItemLookUpEdit1;
            //repositoryItemLookUpEdit1.NullText = "Chọn loại";
            ////InitLookUpEdit();
            //repositoryItemLookUpEdit1.DataSource = products;
            //repositoryItemLookUpEdit1.ValueMember = "MaNhom";
            //repositoryItemLookUpEdit1.DisplayMember = "TenNhom";

            gridView1.Columns[5].Caption = "Nhà sản xuất";
            gridView1.Columns[6].Visible = false;

            gridView1.GroupPanelText = "Đưa cột vào đây để nhóm";
            gridView1.OptionsView.ShowAutoFilterRow = true;

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            gridView1.NewItemRowText = "Nhấn vào đây để thêm mới";
        }

        private void frmProducts_Load(object sender, EventArgs e)
        {
            InitGridProducts();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            frmThemSP frm = new frmThemSP();

            if (frm.ShowDialog(this) == DialogResult.Cancel)
            {
                //MessageBox.Show("Test");
            }
            else
            {
                InitGridProducts();
            }

            frm.Dispose();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            frmThemSP frm = new frmThemSP();
            frm.id = Convert.ToInt32(gridView1.GetFocusedRowCellValue("MaHH"));

            if (frm.ShowDialog(this) == DialogResult.Cancel)
            {
                //MessageBox.Show("Test");
            }
            else
            {
                InitGridProducts();
            }

            frm.Dispose();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var p = db.SO_HANGs.SingleOrDefault(ps => ps.MaHH == gridView1.GetFocusedRowCellValue("MaHH").ToString());
            db.SO_HANGs.DeleteOnSubmit(p);
            db.SubmitChanges();

            gridView1.DeleteSelectedRows();
            gridControl1.Update();
            //InitGridProducts();
        }

        //private void gridView1_InitNewRow(object sender, InitNewRowEventArgs e)
        //{
        //    gridView1.SetFocusedRowCellValue("DVT", "vỉ");
        //    gridView1.SetFocusedRowCellValue("PhePham", "không");
        //    gridView1.SetFocusedRowCellValue("NSX", "Việt Nam");
        //}
    }
}